home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / qex / qexcvsd / mxcom.c < prev    next >
Text File  |  1994-05-24  |  6KB  |  272 lines

  1. /* MXCOM.C -- Library functions for use with ARRL MX-COM MX709 codec card
  2.  *
  3.  * Used with Turbo C V2.0
  4.  *
  5.  * Copyright 1990, American Radio Relay League, Inc.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <dos.h>
  11. #include "mxcom.h"
  12.  
  13. unsigned mxeint, mxdint, mxeover, mxdover;
  14.  
  15. unsigned int MXBASE = 0x300;
  16. static char IRQ = 2;
  17. static struct mxcom
  18. {
  19.     char *flag;
  20.     struct mxbuf *buf[2];
  21.     unsigned cnt;
  22.     int bufnum;
  23. } encode, decode;
  24. static void interrupt (*oldvect)() = NULL;
  25.  
  26. /*  Local functions to set/reset IRQ vector */
  27.  
  28. /*    r e s t i r q
  29. */
  30. static void
  31. restirq(void)
  32. {
  33.     disable();
  34.     outportb(0x21, inportb(0x21) | (1 << IRQ));
  35.     enable();
  36.     if (oldvect != NULL)
  37.     {
  38.         setvect(IRQ+8, oldvect);
  39.         oldvect = NULL;
  40.     }
  41. }
  42.  
  43. /*    s e t i r q
  44. */
  45. static void
  46. setirq(void interrupt (*svc)())
  47. {
  48.     if (oldvect == NULL)
  49.         oldvect = getvect(IRQ+8);
  50.     setvect(IRQ+8, svc);
  51. }
  52.  
  53. /*    m x c a r d
  54.  *
  55.  *    Set card hardware parameters
  56.  *
  57.  *    base    = base address of card (default = 0x300)
  58.  *    irq        = hardware interrupt number (default = 2)
  59.  */
  60. void
  61. mxcard(unsigned base, char irq)
  62. {
  63.     MXBASE = base;
  64.     IRQ = irq & 7;
  65. }
  66.  
  67. /*  m x i n i t
  68.  *
  69.  *    Initialize instruction registers of MX709
  70.  *
  71.  *    dsource    = decoder data source select: IDLE|ENCODER
  72.  *    esource    = encoder audio input source: IDLE|CHANA|CHANB
  73.  *    aout    = channel A output source: DECODER|THROUGH
  74.  *    bout    = channel B output source: DECODER|THROUGH
  75.  *    rate    = codec rate: 0-7
  76.  *    pagesize= power measurement page size: 0-7
  77.  *    sens    = power measurement sensitivity: LOW|HIGH
  78.  *
  79.  *    Notes:
  80.  *        1.    The codec rate in bit/s and the filter cut-off in Hz,
  81.  *            both of which are selected by "rate" depend on the
  82.  *            frequency of the clock (xtal) source.
  83.  *
  84.  *        2.    For both "aout" and "bout" the THROUGH state connects
  85.  *            the A or B input, respectively, to the output.
  86.  *
  87.  *        3.    "pagesize" selects page sizes as follows:
  88.  *                0:  32            4: 160
  89.  *                1:  64            5: 192
  90.  *                2:  96            6: 224
  91.  *                3: 128            7: 256
  92.  *
  93.  *        4.    No error checking is done on passed parameters.
  94.  */
  95. void
  96. mxinit(char dsource, char esource, char aout, char bout, char rate,
  97.     char pagesize, char sens)
  98. {
  99.     char insta = 0, instb = 0;
  100.     char c;
  101.  
  102.     if (dsource == ENCODER)
  103.         insta |= 2;
  104.     if (esource)
  105.     {
  106.         if (esource == CHANB)
  107.             instb |= 8;
  108.     } else
  109.         insta |= 1;
  110.     if (aout == THROUGH)
  111.         instb |= 0x10;
  112.     if (bout == DECODER)
  113.         instb |= 0x20;
  114.     c = rate & 7;
  115.     insta |= (c << 5) | (c << 2);
  116.     instb |= pagesize & 7;
  117.     if (sens == HIGH)
  118.         instb |= 0x80;
  119.     outportb(MX_A, insta);
  120.     outportb(MX_B, instb);
  121. }
  122.  
  123. /*    m x r e s e t
  124.  *
  125.  *    Reset card and interrupt system
  126.  */
  127. void
  128. mxreset(void)
  129. {
  130.     outportb(MX_A, 0);
  131.     outportb(MX_B, 0);
  132.     restirq();
  133. }
  134.  
  135. /*    m y s v c
  136.  *
  137.  *    Local function to service interrupts
  138.  *
  139.  */
  140. static void interrupt
  141. mysvc(void)
  142. {
  143.     char c;
  144.     struct mxbuf *mxb;
  145.  
  146.     c = inportb(MX_STAT);
  147.     if (c & 9)                /* Encoder interrupt */
  148.     {
  149.         mxb = encode.buf[encode.bufnum];
  150.         if (mxb != NULL)
  151.         {
  152.             if (c & 8)
  153.                 mxeover++;
  154.             mxb->dat[encode.cnt++] = inportb(MX_ENC);
  155.             mxeint = encode.cnt;
  156.             if (encode.cnt >= mxb->n)
  157.             {
  158.                 *(encode.flag) = encode.bufnum + 1;
  159.                 encode.bufnum = encode.bufnum ^ 1;
  160.                 encode.cnt = 0;
  161.             }
  162.         }
  163.     }
  164.     if (c & 0x12)            /* Decoder interrupt */
  165.     {
  166.         mxb = decode.buf[decode.bufnum];
  167.         if (mxb != NULL)
  168.         {
  169.             if (c & 0x10)
  170.                 mxdover++;
  171.             outportb(MX_DEC, mxb->dat[decode.cnt++]);
  172.             mxdint = decode.cnt;
  173.             if (decode.cnt >= mxb->n)
  174.             {
  175.                 *(decode.flag) = decode.bufnum + 1;
  176.                 decode.bufnum = decode.bufnum ^ 1;
  177.                 decode.cnt = 0;
  178.             }
  179.         }
  180.     }
  181.     outportb(0x20, 0x20);
  182. }
  183.  
  184. /*    m x _ e n c o d e
  185.  *
  186.  *    Encode audio input and place data in memory.  When called,
  187.  *    mx_encode initiates the encoding process and then returns.
  188.  *    The calling process must monitor the state of the eobflag byte
  189.  *    to determine when the encoding is complete.  One or two buffers
  190.  *    may be used.  If one buffer is used, encoding stops when the
  191.  *    buffer is full.  If two buffers are used. encoding continues
  192.  *    filling alternate buffers until mx_stop is called.
  193.  *
  194.  *    buf1    = pointer to first buffer struct
  195.  *    buf2    = pointer to second buffer struct
  196.  *    eobflag    = pointer to flag byte
  197.  *
  198.  *    Notes:
  199.  *        1.    If buf2 == NULL, encoding will stop after filling buf1;
  200.  *            If buf1 is NULL, no encoding will be performed.
  201.  *
  202.  *        2.    The eobflag byte is written with a 1 when the last byte
  203.  *            of buf1 has been filled and with a 2 when the last byte
  204.  *            of buf2 has been filled.  The calling program may
  205.  *            change the contents of the eobflag byte at will.
  206.  */
  207. void
  208. mx_encode(struct mxbuf *buf1, struct mxbuf *buf2, char *eobflag)
  209. {
  210.     encode.buf[0] = buf1;
  211.     encode.buf[1] = buf2;
  212.     encode.flag = eobflag;
  213.     if (buf1 == NULL)
  214.         return;
  215.     encode.bufnum = 0;
  216.     mxeint = encode.cnt = 0;
  217.     *eobflag = 0;
  218.     setirq(mysvc);
  219.     disable();
  220.     inportb(MX_STAT);
  221.     inportb(MX_ENC);
  222.     outportb(0x21, inportb(0x21) & ~(1 << IRQ));
  223.     outportb(0x20, 0x20);
  224.     enable();
  225. }
  226.  
  227. /*    m x _ d e c o d e
  228.  *
  229.  *    Sends data from memory to codec to be decoded into audio.
  230.  *    mx_decode initiates the process and then returns.  The calling
  231.  *    process must monitor the state of the eobflag byte to
  232.  *    determine when the decoding is complete.
  233.  *
  234.  *    Arguments are the same as for the mx_encode function.
  235.  */
  236.  
  237. void
  238. mx_decode(struct mxbuf *buf1, struct mxbuf *buf2, char *eobflag)
  239. {
  240.     decode.buf[0] = buf1;
  241.     decode.buf[1] = buf2;
  242.     decode.flag = eobflag;
  243.     if (buf1 == NULL)
  244.         return;
  245.     decode.bufnum = 0;
  246.     mxdint = decode.cnt = 1;
  247.     *eobflag = 0;
  248.     setirq(mysvc);
  249.     disable();
  250.     inportb(MX_STAT);
  251.     outportb(0x21, inportb(0x21) & ~(1 << IRQ));
  252.     outportb(0x20, 0x20);
  253.     outportb(MX_DEC, (decode.buf[0])->dat[0]);
  254.     enable();
  255. }
  256.  
  257. /*    m x s t o p
  258.  *
  259.  *    Stop encoder or decoder
  260.  *
  261.  */
  262. void
  263. mxstop(char side)
  264. {
  265.     disable();
  266.     if (side == DECODER)
  267.         decode.buf[0] = decode.buf[1] = NULL;
  268.     else
  269.         encode.buf[0] = encode.buf[1] = NULL;
  270.     enable();
  271. }
  272.